
package com.wyzz.hr.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.ServletRequest;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.BaseAction;

public class EmployeeReportAction extends BaseAction {

	public EmployeeReportAction() {
		reportFileLocation = "";
		reportFormat = "HTML";
	}

	public String execute() throws Exception {
		if (StringUtils.isEmpty(reportFileLocation)) {
			addErrorInfo("\u9519\u8BEF\u7684\u8BF7\u6C42\u53C2\u6570\uFF01");// 错误的请求参数
																				// ！
			return "input";
		}
		ServletRequest request = getRequest();
		if ("PDF".equalsIgnoreCase(reportFormat)) {
			request.setAttribute("_format", "PDF");
			request.setAttribute("_contentType", "attachment");
			request.setAttribute("_fileName", "empReport");
		} else {
			request.setAttribute("_format", "HTML");
		}
		Map params = new HashMap();
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		params.put("startDate", handleDate(startDate));
		params.put("endDate", handleDate(endDate));
		request.setAttribute("_params", params);
		request.setAttribute("_report", reportFileLocation);
		return "success";
	}

	public String getReportFileLocation() {
		return reportFileLocation;
	}

	public void setReportFileLocation(String reportFileLocation) {
		this.reportFileLocation = reportFileLocation;
	}

	public String getReportFormat() {
		return reportFormat;
	}

	public void setReportFormat(String reportFormat) {
		this.reportFormat = reportFormat;
	}

	protected String handleDate(String dateString) {
		String result = "";
		Calendar now = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		if (StringUtils.isEmpty(StringUtils.trim(dateString))) {
			now.set(5, 1);
			result = format.format(now.getTime());
		} else {
			try {
				format.parse(dateString);
				result = dateString;
			} catch (ParseException e) {
				now.set(5, 1);
				result = format.format(now);
			}
		}
		return result;
	}

	private String reportFileLocation;
	private String reportFormat;
}
